package gui.cen.table;

import core.utils.EPara;
import core.utils.Utils;
import gui.Segl0;
import gui.cen.Center;
import gui.processor.Processor;
import gui.processor.Worker;
import gui.utils.Borders;
import gui.utils.Para;
import gui.utils.SliderWrap;
import gui.utils.Slider;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import rpgsdb.db.NK;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Predator extends JPanel implements ActionListener{
    public static int HEIGHT_OF_PREDATOR = 110;
    private JButton process;
    private Center center;
    private JLabel lab;
    private DecimalFormat df = new DecimalFormat("#0.00");
    public Predator(Center center){

        super(new BorderLayout());
        this.center = center;
        this.setPreferredSize(new Dimension(Center.WIDGE_WIDTH, HEIGHT_OF_PREDATOR));
        setBackground(new Color(102, 102, 102));
        setBorder(BorderFactory.createLineBorder(new Color(178, 178, 178), 2));
        JPanel p1 = new JPanel(new GridLayout(3, 1));
        p1.setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
        p1.setOpaque(false);

        p1.add(new SliderWrap(Slider.POSTFILT, 100, 0, EPara.POSTFILT, "Posterior", 75, 3, center, true, 100, new Color(178, 178, 178), new DecimalFormat("#0.00")));
        p1.add(Utils.getButton(process = new JButton("Apply Filter"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Filter reads at this posterior threshold"));
        lab = new JLabel("CURRENT FILTER LEVEL: 0.00");
        lab.setFont(Borders.getFont(Borders.GUIFONT));
        lab.setForeground(Color.RED);
        lab.setBorder(BorderFactory.createEmptyBorder(0,4,0,0));
        p1.add(lab);

        this.add(p1, BorderLayout.SOUTH);
    }

    public void setFilterLVL_label(){
        if(Para.POSTS!=null){lab.setText("CURRENT FILTER LEVEL: "+df.format((double)EPara.POSTFILT/100.00)+"");}
        else{lab.setText("CURRENT FILTER LEVEL: 0.00");}
    }

    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if(source==process){
            ArrayList <NK> projects = Segl0.db.getDataNK(Para.PID);
            if(projects.size()>=2){
                Processor p = new Processor(center);
                p.setTask(Para.TASK_FILTER);
                Worker sl = new Worker(p);
                sl.execute();
            }
            else{JOptionPane.showMessageDialog(null, "Bayesian filter can only be applied if there are 2 or more datasets in project.", "alert", JOptionPane.ERROR_MESSAGE);}
        }
    }
}